package com.kmust.dream.wanderingdream.product.controller;

import com.kmust.dream.wanderingdream.comm.pojo.ResultBean;
import com.kmust.dream.wanderingdream.product.entity.ProductImage;
import com.kmust.dream.wanderingdream.product.feign.UploadFeign;
import com.kmust.dream.wanderingdream.product.vo.ProductImageBatchVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.kmust.dream.wanderingdream.product.service.ProductImageService;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


@RestController
@RequestMapping("/product/productImage")
@Api(value = "商品预览图API")
public class ProductImageController {

    @Autowired
    ProductImageService productImageService;

    @Autowired
    UploadFeign uploadFeign;

    /**
     * 查找商品对应的橱窗图片
     *
     * @param productId 商品id
     * @return resultBean
     */
    @ApiOperation(value = "查找商品对应的预览图片", notes = "参数是商品id")
    @GetMapping("/getProductImageByProductId/{productId}")
    public ResultBean getProductImageByProductId(@PathVariable Integer productId) {
        return productImageService.getProductImageById(productId);
    }

    /**
     * 添加商品的橱窗图片
     * @param file 文件对象
     * @param productId 商品id
     * @return resultBean
     */
    @ApiOperation(value = "添加商品的橱窗图片", notes = "参数是商品预览图片json")
    @PostMapping("/addProductImage")
    public ResultBean addProductImage(@RequestParam("file") MultipartFile file, @RequestParam("productId") Integer productId) {
        String url = this.getUrl(file);
        return productImageService.addProductImage(productId,url);
    }

    @ApiOperation(value = "更新商品的预览图", notes = "参数是商品预览图片json")
    @PutMapping("/updateProductImage")
    public ResultBean updateProductImage(@RequestBody ProductImage productImage) {
        return productImageService.updateProductImage(productImage);
    }

    @ApiOperation(value = "删除商品的预览图", notes = "参数是商品预览图片主键")
    @DeleteMapping("/deleteProductImage/{productImageId}")
    public ResultBean deleteProductImage(@PathVariable Integer productImageId) {
        return productImageService.deleteProductImage(productImageId);
    }

    @PostMapping("/addProductImageBatch")
    public ResultBean addProductImageBatch(@RequestBody ProductImageBatchVO productImageBatchVO) {
        return productImageService.addProductImageBatch(productImageBatchVO);
    }

    @PostMapping("/getUrl")
    public String getUrl(@RequestParam("file") MultipartFile file) {
        return uploadFeign.getUrl(file);
    }
}
